package org.gfix.binding;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.gfix.annotation.FixMessageBinding;

class ClassLevelBinding {

	public enum MatchResult{NOT_MATCHED, MATCHED};
	
	private Map<Integer, Criteria> interestedTags = new HashMap<Integer, Criteria>();
	private List<FixEvent> output = new ArrayList<FixEvent>(2);
	
	void add(FixMessageBinding fmb)
	{
		Criteria crit = null;
		switch(fmb.matchType())
		{
			case EQUALS:
				crit = new EqualsCriteria(fmb.matches());
			break;
			
		}
		interestedTags.put(fmb.fixTag(),crit);
		output.add(new FixEvent(fmb.fixTag(), fmb.sending()));
	}
	
	public Collection<Integer> getBoundTags()
	{
		return interestedTags.keySet();
	}
	
	public int getNumberOfboundTags()
	{
		return interestedTags.size();
	}
	
	public MatchResult evaluate(int tag, String contents)
	{
		return interestedTags.get(tag).evaluate(contents);
	}
	
	public void serialise(Object object, MessageSink sink)
	{
		for(FixEvent fe: output)
		{
			sink.writeTag(fe.tag, fe.content);
		}
	}
	
	private interface Criteria
	{
		MatchResult evaluate(String contents);
	}
	
	private class EqualsCriteria implements Criteria
	{
		String match;
		
		
		EqualsCriteria(String match) {
			super();
			this.match = match;
		}


		public MatchResult evaluate(String contents)
		{
			if(match.equals(contents))
			{
				return MatchResult.MATCHED;
			}
			else
			{
				return MatchResult.NOT_MATCHED;
			}
		}
	}
}
